<style type="text/css">
    td.old      {
        color: red;
    }
    
    td.next    {
        color: blue;
    }
</style>

<?php 
if(!$_SESSION['leider'] || !$index)
{
    include('404.php');
}
else
{
?>
    <script type="text/javascript">
        function checkboxDisable() {
            var mycheckbox = document.getElementById('delcheckbox').checked;
            if(mycheckbox)
            {
                document.getElementById('deletejabutton').disabled = false;
            }
            else
            {
                document.getElementById('deletejabutton').disabled = true;
            }
        }
    </script>
<?php
if(isset($_POST['delprogrammaja']))
{
    // a = prog_programmas
    // b = prog_benodigdheden
    $ps = new PreparedStatement("DELETE a, b FROM prog_programmas a LEFT JOIN prog_benodigdheden b ON a.programmaid = b.prog_programmaid WHERE programmaid = ?;");
    $ps->setInt($_POST['deleteprogrammavalue']);
    $ps->execute();
}
else if(isset($_GET['action']))
    if($_GET['action'] == 'delete')
    {
        echo "<br /><center>Weet u zeker dat je dit programma wil verwijderen?<br /><br />";
        echo "<input onclick='checkboxDisable(this)' id='delcheckbox' type='checkbox' /> Ja ik weet het zeker.<br />";
        echo "<form name='deletef1' action='?p=programmas' style='display:inline;' method='post'><input name='delprogrammaja' type='hidden' value='delprogrammaja' /><input type='hidden' name='deleteprogrammavalue' value='{$_GET['value']}' /><input id='deletejabutton' disabled='disabled' type='submit' value='Ja' /></form>";
        echo "<form name='deletef2' action='?p=programmas' style='display:inline;' method='post'><input type='hidden' value='delnee' /><input type='submit' value='Nee' /></form></center>";
    }
if(!isset($_GET['action']))
{
?>
<link rel="stylesheet" type="text/css" href="styles/inschrijvingen.css" />
<center>
<h2>Programma's</h2>
<?php
$ps = new PreparedStatement("SELECT leidingid FROM leiding WHERE leidinggroepid = ?;");
$ps->setInt($_SESSION['leider']);
$q = $ps->execute();
$leidingids = "";
while($row = mysql_fetch_array($q))
{
    if($leidingids == "")
        $leidingids .= "'" . $row['leidingid'] . "'";
    else
        $leidingids .= ", '" . $row['leidingid'] . "'";
}

// a = prog_programmas
// b = prog_benodigdheden
$ps = new PreparedStatement("SELECT *, CONCAT(voornaam, ' ', IFNULL(tussenvoegsels, ' '), ' ', achternaam) AS naam, COUNT(b.prog_benodigdheid) boolbenodigd FROM (prog_programmas a INNER JOIN leiding ON a.programmaleiding = leiding.leidingid) LEFT JOIN prog_benodigdheden b ON a.programmaid = b.prog_programmaid WHERE leidingid IN ($leidingids) GROUP BY a.programmaid ORDER BY programmadatum DESC LIMIT 20;");
$q = $ps->execute();
echo "<table>";
echo "<tr><td style='width: 100px;' class='toprow'>Datum: </td><td style='width: 250px;' class='toprow'>Programma: </td><td style='width: 150px;' class='toprow'>Leiding: </td><td class='toprow'>Benodigdheden:</td><td style='width: 50px;'></td></tr>";
while($row = mysql_fetch_array($q))
{
    $class = 'current';
    $date = date('Y') . "-" . date('m') . "-" . date('d');
    $nextdate = date('Y') . "-" . date('m') . "-" . (date('d') + 7);
    if($date > $row['programmadatum'])
        $class = 'old';
    else if($date < $row['programmadatum'] && $row['programmadatum'] < $nextdate)
        $class = 'next';
    echo "<tr>
              <td class='$class'>{$row['programmadatum']}</td>
              <td class='$class'>{$row['programmanaam']}</td>
              <td class='$class'>{$row['naam']}</td>
              <td class='$class'>{$row['boolbenodigd']}</td>
              <td><img src='images/icons/settings.gif' /> <a href='?p=programmas&action=delete&value={$row['programmaid']}'><img src='images/icons/minus.gif' /></a></td>
         </tr>";
}
echo "</table><br />";
echo "<a href='?p=addprogram'>Programma toevoegen <img src='images/icons/buttonplus.gif' /></a>";
echo "</center>";


}
}
?>